<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../../dashboard/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions( {xdata,ydata,zdata,jdata }  = {}) {
      this.chart.setOption({
        color: ['#3398DB'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
    //     legend: {
    //     data: ['专用车', '自卸车','牵引车'],
    //     right: '6',
    //     top: "10",
    //     textStyle: {
    //             fontSize: 15,
    //             color: "#00FFE3"
    //             }
    // },
        title: {
            text: zdata,
            textStyle: {
              fontSize: 15,
              color: "#00FFE3"
            },
            left: '6',
            top: "10"
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: xdata,
            axisTick: {
                alignWithLabel: true
            }
        },
        yAxis:{
            type: 'value'
        },
        series: [
            {
                name: '差错率',
                type: 'line',
                data: ydata,
                markLine: {
                  silent: true,
                  lineStyle: {
                    normal: {
                      color: '#01fef9'                   // 这儿设置安全基线颜色
                    }
                  },
                  data: [
                    {
                      type: 'average', 
                      name: '平均值'
                    }
                  //   {
                  //     yAxis: jdata
                  // }
                  ],
                  label: {
                    normal: {
                      formatter: jdata+' (平均值)',             // 这儿设置安全基线
                      position: 'insideEndTop'           
                    }
                  },
                },
            }
        ]
     })
    }
  }
}
</script>
